package com.baturu.vin.benz.cleanData;

import com.baturu.vin.benz.BaseTest;
import com.baturu.vin.benz.cleanData.dao.BenzCleanDAO;
import com.baturu.vin.benz.dal.dao.BenzImageDAO;
import com.baturu.vin.benz.dto.EPCImageViewerCalloutArray;
import com.baturu.vin.benz.dto.ImageExtractorFV4;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.Date;
import java.util.List;

/**
 * 从transbase数据库获取奔驰图片
 * @author jiangxinlei
 * @Time 2017/3/2.
 */
public class BenzImageOutput extends BaseTest {

    @Autowired
    private BenzImageDAO benzImageDAO;
    @Autowired
    private BenzCleanDAO cleanDAO;

    @Test
    public void getImageCalloutPosition() throws ClassNotFoundException, SQLException, IOException {
        cleanDAO.createTable("benz_callout_position");
        getImageCalloutPosition("jdbc:transbase://192.168.16.192:2034/BM_IMAGES");
        getImageCalloutPosition("jdbc:transbase://192.168.16.192:2034/BM_IMAGES_ARC");



        cleanDAO.updateCalloutPosition1();
        cleanDAO.updateCalloutPosition2();

        cleanDAO.dropBenzCalloutPosAdd();
        cleanDAO.createBenzCalloutPosAdd();
        cleanDAO.addCalloutPosAdd();
        cleanDAO.dropMissingImage();
        cleanDAO.createMissingImage();

        getAddImage();
    }

    private void getAddImage() throws ClassNotFoundException, SQLException, IOException {
        getImage("jdbc:transbase://192.168.16.192:2034/BM_IMAGES");
        getImage("jdbc:transbase://192.168.16.192:2034/BM_IMAGES_ARC");
    }

    private void getImage(String dburl1) throws ClassNotFoundException, SQLException, IOException {
        List<String> missingPics = benzImageDAO.getMissingPic();
        Class.forName("transbase.jdbc.Driver");
        String uname1 = "tbadmin";
        String pw1 = "LMtdKNkhDHakNR3";
        Connection conn= DriverManager.getConnection(dburl1,uname1,pw1);
        for (String picName : missingPics) {
            String query = "select IMAGE_NAME,IMAGE from IMAGE_V WHERE IMAGE_NAME = \'"+picName+ "\'" ;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            if (rs == null) {
                rs.close();
                stmt.close();
                conn.close();
                return;
            }
            int count = 0;
            while(rs.next()){
                String imageName = rs.getString(1);
                byte[] imageByte = null;
                try {
                    imageByte = rs.getBytes(2);
                } catch (Exception e) {
                    continue;
                }
                imageByte = rs.getBytes(2);
                String catalog = imageName.substring(0,6);
                EPCImageViewerCalloutArray calloutArray = new EPCImageViewerCalloutArray();
                ImageExtractorFV4 imgExtractor = new ImageExtractorFV4();
                Image img = imgExtractor.getImage(imageByte, calloutArray);
                String filePath = "d:/benzImage/"+catalog;
                makeDirs(filePath);
                ImageIO.write((RenderedImage) img, "png", new File(filePath+"/"+imageName+".png"));
                count++;
                System.out.println("处理到第"+count+"张图片,名称为:"+imageName);
            }
        }
    }

    private void makeDirs(String filePath){
        File file = new File(filePath);
        if(!file.exists()) {
            file.mkdirs();
        }
        return;
    }


    private void getImageCalloutPosition(String dburl1) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("transbase.jdbc.Driver");
        String uname1 = "tbadmin";
        String pw1 = "LMtdKNkhDHakNR3";
        Connection conn= DriverManager.getConnection(dburl1,uname1,pw1);
        String query = "select IMAGE_NAME,IMAGE from IMAGE_V";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        if (rs == null) {
            rs.close();
            stmt.close();
            conn.close();
            return;
        }
        int cou = 0;
        StringBuilder sb = new StringBuilder();
        while(rs.next()){
            String imageName = rs.getString(1);
            System.out.println("处理到第"+cou+"张图片,名称为:"+imageName);
            byte[] imageByte = null;
                imageByte = rs.getBytes(2);
            String catalog = imageName.substring(0,6);
            EPCImageViewerCalloutArray calloutArray = new EPCImageViewerCalloutArray();
            ImageExtractorFV4 imgExtractor = new ImageExtractorFV4();
            Image img = imgExtractor.getImage(imageByte, calloutArray);
            for(int i=0;i<calloutArray.getCount();i++){
                String callout = calloutArray.getCalloutDesc(i);
                int x = calloutArray.getX(i);
                int y = calloutArray.getY(i);
                int topLeftX = x - 12;
                int topLeftY = y - 10;
                int bottomRightX = x + 12;
                int bottomRightY = y + 10;
                sb.append("('").append(imageName).append("','").append(callout).append("',").append(topLeftX).append(",").append(topLeftY).append(",").append(bottomRightX).append(",").append(bottomRightY).append("),");
            }
            cou++;

            if(cou % 1000 == 0){
                if(StringUtils.isNotBlank(sb.toString())){
                    sb.deleteCharAt(sb.length()-1);
                    System.out.println("开始第"+cou/1000+"次insert,"+new Date());
                    benzImageDAO.insertBenzImagePosition(sb.toString());
                    System.out.println("结束"+new Date());
                    sb = new StringBuilder();
                }
            }
        }
        if(StringUtils.isNotBlank(sb.toString())){
            sb.deleteCharAt(sb.length()-1);
            benzImageDAO.insertBenzImagePosition(sb.toString());
        }
        rs.close();
        stmt.close();
        conn.close();
    }

}
